package cn.edu.scujj.view;

import cn.edu.scujj.dao.TongjiDao;
import cn.edu.scujj.bean.TongJi;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.List;
import java.util.Vector;

public class TongJiUI extends JFrame {
    JScrollPane jScrollPane = new JScrollPane();
    AbstractTableModel tableModel;
    Vector<TongJi> vector = new Vector<>(); // 明确指定泛型类型为 TongJi
    TongjiDao dao = new TongjiDao();

    JTextField txtName = new JTextField(10);
    JTextField txtNum = new JTextField(10);
    JTextField txtMoney = new JTextField(10);
    JTextField txtProfit = new JTextField(10);
    JTextField txtInventory = new JTextField(10);

    public TongJiUI() {
        setTitle("销售出库");
        setBounds(100, 100, 500, 300);
        setLayout(new BorderLayout());

        JPanel panelUP = new JPanel();
        JPanel panelDown = new JPanel();
        JPanel panelCenter = new JPanel();

        panelUP.add(jScrollPane);
        createTable();

        add(panelUP, BorderLayout.NORTH);
        add(panelCenter, BorderLayout.CENTER);
        add(panelDown, BorderLayout.SOUTH);

        panelCenter.add(new JLabel("商品名称："));
        panelCenter.add(txtName);
        panelCenter.add(new JLabel("商品数量："));
        panelCenter.add(txtNum);
        panelCenter.add(new JLabel("商品单价："));
        panelCenter.add(txtMoney);
        panelCenter.add(new JLabel("商品利润："));
        panelCenter.add(txtProfit);
        panelCenter.add(new JLabel("商品库存："));
        panelCenter.add(txtInventory);

        JButton btnAdd = new JButton("添加");
        btnAdd.addActionListener(e -> addData());

        JButton btnDelete = new JButton("删除");
        btnDelete.addActionListener(e -> deleteData());

        JButton btnUpdate = new JButton("修改");
        btnUpdate.addActionListener(e -> updateData());

        JButton btnReset = new JButton("重置");
        btnReset.addActionListener(e -> {
            txtNum.setText("");
            txtName.setText("");
            txtMoney.setText("");
            txtProfit.setText("");
            txtInventory.setText("");
        });

        JButton btnRefresh = new JButton("刷新");
        btnRefresh.addActionListener(e -> refreshData());

        panelDown.add(btnAdd);
        panelDown.add(btnDelete);
        panelDown.add(btnUpdate);
        panelDown.add(btnReset);
        panelDown.add(btnRefresh);

        select();
    }

    private void createTable() {
        String[] columnNames = {"商品名称", "商品数量", "商品单价", "商品利润", "商品库存"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);

        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (rowIndex < vector.size()) {
                    TongJi tj = vector.get(rowIndex);
                    switch (columnIndex) {
                        case 0: return tj.getName();
                        case 1: return tj.getNum();
                        case 2: return tj.getMoney();
                        case 3: return tj.getProfit();
                        case 4: return tj.getInventory();
                        default: return null;
                    }
                }
                return null;
            }
        };

        table.setModel(tableModel);
    }

    private void addData() {
        try {
            TongJi tj = new TongJi();
            tj.setName(txtName.getText());
            tj.setNum(Double.parseDouble(txtNum.getText()));
            tj.setMoney(Float.parseFloat(txtMoney.getText()));
            tj.setProfit(Float.parseFloat(txtProfit.getText()));
            tj.setInventory(Double.parseDouble(txtInventory.getText()));

            if (dao.addTongji(tj)) {
                JOptionPane.showMessageDialog(this, "添加成功");
                refreshData();
                resetFields();
            } else {
                JOptionPane.showMessageDialog(this, "添加失败");
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入正确的数字格式");
        }
    }
private void deleteData() {
    if (txtName.getText().isEmpty()) {
        JOptionPane.showMessageDialog(this, "请选择要删除的记录");
        return;
    }

    if (dao.deleteTongji(txtName.getText())) {
        JOptionPane.showMessageDialog(this, "删除成功");
        refreshData();
    } else {
        JOptionPane.showMessageDialog(this, "删除失败");
    }
}

private void updateData() {
    if (txtName.getText().isEmpty()) {
        JOptionPane.showMessageDialog(this, "请选择要修改的记录");
        return;
    }

    try {
        TongJi tj = new TongJi();
        tj.setName(txtName.getText());
        tj.setNum(Double.parseDouble(txtNum.getText()));
        tj.setMoney(Float.parseFloat(txtMoney.getText()));
        tj.setProfit(Float.parseFloat(txtProfit.getText()));
        tj.setInventory(Double.parseDouble(txtInventory.getText()));

        if (dao.updateTongji(tj)) {
            JOptionPane.showMessageDialog(this, "修改成功");
            refreshData();
            resetFields();
        } else {
            JOptionPane.showMessageDialog(this, "修改失败");
        }
    } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(this, "请输入正确的数字格式");
    }
}

private void refreshData() {
    vector.clear();
    vector.addAll(dao.findAll());
    tableModel.fireTableDataChanged();
}

private void resetFields() {
    txtName.setText("");
    txtNum.setText("");
    txtMoney.setText("");
    txtProfit.setText("");
    txtInventory.setText("");
}

void select() {
    refreshData();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> new TongJiUI().setVisible(true));
}
}